<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use dosamigos\datetimepicker\DateTimePicker;
use common\helper\HashHelper;
$time = time();
$key = HashHelper::getUploadHash($time);
/* @var $this yii\web\View */
/* @var $model common\models\TryRec */
/* @var $form yii\widgets\ActiveForm */
?>
<style>
.field-tryrec-recommend_time .input-group{width:300px;}
.field-tryrec-display_position .form-control{width:300px;}
.field-tryrec-status .form-control{width:300px;}
</style>
<div class="try-rec-form">

    <?php $form = ActiveForm::begin(['id' => 'form1']); ?>

    <?= $form->field($model, 'display_position')->dropDownList([1=>'第一行',2=>'第二行',3=>'第三行',4=>'第四行',]) ?>

    <?= $form->field($model, 'status')->dropDownList([1=>'显示',0=>'不显示']) ?>

    <?= $form->field($model, 'product1')->textInput() ?>

    <?= $form->field($model, 'product2')->textInput() ?>

    <?= $form->field($model, 'product3')->textInput() ?>

    <?= $form->field($model, 'pic1')->widget('yidashi\webuploader\WebBatchUploader',['server'=>common\helper\UrlManager::createUrl(Yii::$app->params['domains']['upload'],['upload/webupload','time'=>$time,'key'=>$key]),'options'=>['number'=>3]]) ?>

    <!--?= $form->field($model, 'pic2')->widget('yidashi\webuploader\Webuploader',['server'=>\Yii::$app->urlManager->createUrl(['upload/webupload'])]) ?-->

    <!--?= $form->field($model, 'pic3')->widget('yidashi\webuploader\Webuploader',['server'=>\Yii::$app->urlManager->createUrl(['upload/webupload'])]) ?-->

    <?= $form->field($model, 'recommend_time')->widget(DateTimePicker::className(), 
        [
        'template'=>"{input}{reset}{button}",
        'clientOptions' => [
            'autoclose' => true,
            'linkFormat' => 'yyyy-MM-dd', // if inline = true
            //'format' => 'yyyy-mm-dd', // if inline = false
            'todayBtn' => true,
            'pickerPosition'=>"bottom-left",
            'language'=>'zh-CN',
            'minView'=>'hour'
        ]    
    ])  ?>

    <div class="form-group">
        <?= Html::input('hidden', 'form_check1', $model->isNewRecord ? 0 : 1, ['id'=>'form_check1']); ?>
        <?= Html::input('hidden', 'form_check2', $model->isNewRecord ? 0 : 1, ['id'=>'form_check2']); ?>
        <?= Html::input('hidden', 'form_check3', $model->isNewRecord ? 0 : 1, ['id'=>'form_check3']); ?>
        <?= $model->isNewRecord ? $form->field($model, 'add_time')->hiddenInput(['value'=>date('Y-m-d H:i:s')])->label(false) : '' ?>
        <?= $model->isNewRecord ? $form->field($model, 'add_user')->hiddenInput(['value'=>Yii::$app->session['admin_name']])->label(false)  : '' ?>
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<?php $this->beginBlock('js');?>
<script>
$(function(){
    $('#tryrec-product1,#tryrec-product2,#tryrec-product3').change(function(){
            var product_id = 0;
            var _this = $(this);
            if($(this).attr('id') == 'tryrec-product1') product_id = $('#tryrec-product1').val();
            else if($(this).attr('id') == 'tryrec-product2') product_id = $('#tryrec-product2').val();
            else if($(this).attr('id') == 'tryrec-product3') product_id = $('#tryrec-product3').val();
           $.post("<?=Yii::$app->urlManager->createUrl('day-try/get-goods-info')?>",{product_id: product_id, rnd:Math.random()},  function(json) {
               if(json.flag == 1)
               {
                   if(_this.attr('id') == 'tryrec-product1')$('#form_check1').val(1);
                   else if(_this.attr('id') == 'tryrec-product2')$('#form_check2').val(1);
                   else if(_this.attr('id') == 'tryrec-product3')$('#form_check3').val(1);
                   return false;
               }else
               {
                   alert(json.msg);
                   if(_this.attr('id') == 'tryrec-product1')$('#form_check1').val(0);
                   else if(_this.attr('id') == 'tryrec-product2')$('#form_check2').val(0);
                   else if(_this.attr('id') == 'tryrec-product3')$('#form_check3').val(0);
                   return false;
               }
            },"json");
    });
    
    $("#form1").submit(function(){
        
        if($('#form_check1').val() == 0)
        {
            alert('货品1不存在');
            return false;  
        }else if($('#form_check2').val() == 0)
        {
            alert('货品2不存在');
            return false;  
        }else if($('#form_check3').val() == 0)
        {
            alert('货品3不存在');
            return false;  
        }
        var tryrec_pic1 = $('#tryrec-pic1').val();
        tryrec_pic1 = tryrec_pic1.split(',');
        if(tryrec_pic1.length < 3)
        {
            alert('必须上传3张图片');
            return false;
        }
           return true;  
    });  
});

</script>
<?php $this->endBlock(); ?>